package com.link2room.ironhide.service.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.IronhideSysExceptionEnum;
import com.link2room.ironhide.dto.result.manager.L2RServiceDto;
import com.link2room.ironhide.entity.L2RGroupService;
import com.link2room.ironhide.entity.L2RHotelService;
import com.link2room.ironhide.exception.IronhideException;
import com.link2room.ironhide.service.IL2RServicesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sss on 2016/10/12.
 */
@Service("l2RServicesService")
public class L2RServicesServiceImpl implements IL2RServicesService {
    @Resource
    private ICommonService commonService;

    @Override
    public <S>List<L2RServiceDto> listL2RServices(String lhotelGroupCode,
                                                  String lhotelCode,
                                                  S queryObj,
                                                  Class<S> sourceClass, boolean isHotel)throws Exception{
        QueryResult<S> l2rServicesResult = commonService.findAllEQ(sourceClass, queryObj);
        QueryResult<L2RServiceDto> positionBaseDtoResult= ClassConverUtil.copyQueryResultToDtoQueryResult(l2rServicesResult,L2RServiceDto.class, false);
        List<L2RServiceDto> l2RServiceDtos = positionBaseDtoResult.getResultlist();
        List<L2RServiceDto> topList=new ArrayList<>();
        List<L2RServiceDto> tempList=new ArrayList<>();

        for(L2RServiceDto l2RServiceDto:l2RServiceDtos){
            l2RServiceDto.setLhotelGroupCode(lhotelGroupCode);
            l2RServiceDto.setId(null);
            if("ROOT".equals(l2RServiceDto.getServiceType())){
                l2RServiceDto.setServiceKey(this.getServiceKey(l2RServiceDto.getServiceType(),l2RServiceDto.getServiceCode()));
                //取出顶级节点
                topList.add(l2RServiceDto);
            }else {
                //保存子节点
                tempList.add(l2RServiceDto);
            }
        }
        for(L2RServiceDto top:topList){
            List<L2RServiceDto> childList=childNodeList(tempList,top.getServiceCode());
            top.setChildren(childList);
        }
        return topList;
    }

    @Override
    public void saveOrUpdateL2RServiceDto(Object[] delIds, List<L2RServiceDto> l2RServiceDtoList)throws Exception{
        commonService.delete(L2RGroupService.class,delIds);
        commonService.saveOrUpdateList(ClassConverUtil.copyPropertiesList(l2RServiceDtoList,L2RGroupService.class));
    }

    @Override
    @Transactional
    public void saveOrUpdateHotelServices(String lhotelGroupCode, String lhotelCode, List<L2RHotelService> l2RHotelServiceList)throws Exception {
        if(StringUtil.isNotBlank(lhotelCode)){
            L2RHotelService queryL2RHP = new L2RHotelService();
            queryL2RHP.setLhotelGroupCode(lhotelGroupCode);
            queryL2RHP.setLhotelCode(lhotelCode);
            commonService.deleteAllEQ(L2RHotelService.class, queryL2RHP);
            commonService.saveOrUpdateList(l2RHotelServiceList);
        }else {
            throw new IronhideException(IronhideSysExceptionEnum.HOTELS_NOT_EXIT);
        }
    }

    private List<L2RServiceDto> childNodeList(List<L2RServiceDto> inList, String parentCode)throws Exception{
        List<L2RServiceDto> mainList=new ArrayList<>();
        List<L2RServiceDto> tempList=new ArrayList<>();

        for(L2RServiceDto in:inList){
            if(parentCode.equals(in.getServiceType())){
                in.setServiceKey(this.getServiceKey(in.getServiceType(),in.getServiceCode()));
                mainList.add(in);
            }else {
                tempList.add(in);
            }
        }

        for(L2RServiceDto main:mainList){
            List<L2RServiceDto> childList=childNodeList(tempList,main.getServiceCode());
            main.setChildren(childList);
        }
        return mainList;
    }

    private String getServiceKey(String parentCode,String childCode){
        return parentCode+ DefaultValue.CUT+childCode;
    }

}
